﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Bpt;
using ESOL.Business.Prod;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanOrderRecreate : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private ProdPlanOrderEntity mSelectedPlanOrder;
        #endregion

        #region ctors
        public frmMaintainProductPlanOrderRecreate()
        {
            InitializeComponent();
        }
        public frmMaintainProductPlanOrderRecreate(ProdPlanOrderEntity entity)
        {
            InitializeComponent();
            mSelectedPlanOrder = entity;
        }
        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "POI", false);

            List<MstCommonItemEntity> list1 = MstCommonItemBs.GetMstCommonItemListByType("POI");
            repositoryItemLookUpEdit1.DataSource = list1;
            repositoryItemLookUpEdit1.ValueMember = "ItemCode";
            repositoryItemLookUpEdit1.DisplayMember = "ItemName";

            DataTable dtIsMark = new DataTable();
            dtIsMark.Columns.Add("name");
            dtIsMark.Columns.Add("value");
            dtIsMark.Rows.Add("是", 1);
            dtIsMark.Rows.Add("否", 0);
            repositoryItemLookUpEdit2.DataSource = dtIsMark;
            repositoryItemLookUpEdit2.ValueMember = "value";
            repositoryItemLookUpEdit2.DisplayMember = "name";
        }

        private void SetField()
        {
            if (mSelectedPlanOrder == null)
                return;
            MstProductEntity mProduct;
            SalesProdPlanApplicationItemEntity mApplicationItem;
            mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mSelectedPlanOrder.ApplicationItemId);
            if (mApplicationItem == null)
                return;
            mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
            if (mProduct == null)
                return;
            txtProductPlanOrder.Text = mSelectedPlanOrder.PlanOrderCode;
            txtProductCode.Text = mProduct.ProductCode;
            txtProductName.Text = mProduct.ProductName;
            txtPlanQty.Text = mSelectedPlanOrder.Qty.ToString();
            txtContractModel.Text = mApplicationItem.ProductSpecOut;
            cmbStatus.SetSelectedValue(mSelectedPlanOrder.Status);
            dtpDeliveryDatetime.EditValue = mSelectedPlanOrder.DeliveryDate;
        }

        private void BindGridData()
        {
            DataTable dtWorkingOrder = new DataTable();
            dtWorkingOrder.Columns.Add("PLAN_ORDER_ITEM_ID");
            dtWorkingOrder.Columns.Add("PLAN_ORDER_ID");
            dtWorkingOrder.Columns.Add("BOM_ID");
            dtWorkingOrder.Columns.Add("RELATED_PLAN_ORDER_ITEM_ID");
            dtWorkingOrder.Columns.Add("PRODUCT_ID");
            dtWorkingOrder.Columns.Add("BLUEPRINT_ID");
            dtWorkingOrder.Columns.Add("TECHNIC_PERSON");
            dtWorkingOrder.Columns.Add("ORDER_ITEM_CODE");
            dtWorkingOrder.Columns.Add("DELIVERY_DATE", typeof(DateTime));
            dtWorkingOrder.Columns.Add("STORAGE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("PRODUCTED_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("PURCHASE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("STATUS");
            dtWorkingOrder.Columns.Add("REMARK");
            dtWorkingOrder.Columns.Add("PRODUCT_TYPE");
            dtWorkingOrder.Columns.Add("WORKORDER_CREATE_DATE");
            dtWorkingOrder.Columns.Add("MATERIAL_TOTAL_WEIGHT");
            dtWorkingOrder.Columns.Add("MATERIAL_TOTAL_QTY");
            dtWorkingOrder.Columns.Add("ORDER_TYPE");
            dtWorkingOrder.Columns.Add("IS_ASSEMBLY");
            dtWorkingOrder.Columns.Add("IS_PRINTED");
            dtWorkingOrder.Columns.Add("IS_MARK");
            dtWorkingOrder.Columns.Add("PLAN_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("ACTUAL_STORAGE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("MOVE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("PRODUCT_PLAN_ORDER");
            dtWorkingOrder.Columns.Add("PRODUCT_NAME");
            dtWorkingOrder.Columns.Add("DESCRIPTION");
            gvMain.DataSource = dtWorkingOrder;
        }

        private bool CheckData()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("明细不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrEmpty(dr["QTY"].ToString()))
                {
                    MessageBox.Show("生产数量不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
            BindGridData();
        }

        private void tsmNewLine_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
        }

        private void tsmDelLine_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            if (gridView1.GetSelectedRows().Length == 0)
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            dt.Rows.RemoveAt(gridView1.GetSelectedRows()[0]);
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainMBOMQuery frm = new frmMaintainMBOMQuery(mSelectedPlanOrder);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProductBomMaster != null)
            {
                DataTable dtWorkingOrder = gvMain.DataSource as DataTable;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(frm.SelectedProductBomMaster.ProductId);
                DataRow drWorkingOrder = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                drWorkingOrder["PLAN_ORDER_ITEM_ID"] = Guid.NewGuid().ToString();
                drWorkingOrder["PLAN_ORDER_ID"] = mSelectedPlanOrder.PlanOrderId;
                drWorkingOrder["BOM_ID"] = frm.SelectedProductBomMaster.BomId;
                drWorkingOrder["PRODUCT_ID"] = mProduct.ProductId;
                BptBlueprintEntity mBlueprint = BptBlueprintBs.GetBptBlueprintByProductID(mProduct.ProductId);
                drWorkingOrder["BLUEPRINT_ID"] = mBlueprint.BlueprintId;
                drWorkingOrder["TECHNIC_PERSON"] = Common.CommonParameter.CurrentUser.PersonId;
                drWorkingOrder["DELIVERY_DATE"] = mSelectedPlanOrder.DeliveryDate;
                decimal dPlanQty = mSelectedPlanOrder.Qty * Convert.ToDecimal(frm.SelectedProductBomMaster.UnitDose);
                drWorkingOrder["PLAN_QTY"] = dPlanQty;
                drWorkingOrder["QTY"] = dPlanQty;
                drWorkingOrder["PRODUCTED_QTY"] = 0;
                drWorkingOrder["STATUS"] = "POI01";
                drWorkingOrder["PRODUCT_TYPE"] = "PDTYPE01";
                drWorkingOrder["ORDER_TYPE"] = "POIT01";
                drWorkingOrder["PRODUCT_NAME"] = mProduct.ProductName;
                drWorkingOrder["DESCRIPTION"] = mProduct.Description;
                drWorkingOrder["IS_MARK"] = mSelectedPlanOrder.IsMark;
                drWorkingOrder["WORKORDER_CREATE_DATE"] = DateTime.Now;

            }
        }

        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            DataTable dt = gvMain.DataSource as DataTable;
            string sErrMsg = "";
            if (ProdPlanOrderItemBs.WorkingOrderRemakeOrder(dt, out sErrMsg))
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(sErrMsg + "保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //base.Click_Save();
        }

        #endregion
    }
}
